﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace BSE.Windows.Test
{
    public class DataGridViewComboEditBoxCell : DataGridViewComboBoxCell
    {
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            
            ComboBox comboBox = (ComboBox)base.DataGridView.EditingControl;

            if (comboBox != null)
            {
                comboBox.DropDownStyle = ComboBoxStyle.DropDown;
                //comboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
                
                comboBox.TextChanged -= new EventHandler(comboBox_TextChanged);
                comboBox.TextChanged += new EventHandler(comboBox_TextChanged);
            }
        }

        protected void comboBox_Leave(object sender, EventArgs args)
        {
            DataGridViewComboBoxEditingControl cbo = (DataGridViewComboBoxEditingControl)sender;
            if (cbo.Text.Trim() == string.Empty) return;

            DataGridView grid = cbo.EditingControlDataGridView;
            string value = cbo.Text;

            cbo.Leave -= new EventHandler(comboBox_Leave);

            for (int i = 0; i < value.Length; i++)
            {
                if ((value[i] >= 'a' && value[i] <= 'z') || (value[i] >= 'A' && value[i] <= 'Z'))
                    return;
            }

            // Add value to list if not there
            if (cbo.Items.IndexOf(value) == -1)
            {
                DataGridViewComboBoxColumn cboCol = (DataGridViewComboBoxColumn)grid.Columns[grid.CurrentCell.ColumnIndex];
                // Must add to both the current combobox as well as the template, to avoid duplicate entries
                Person p = new Person();
                p.PersonName = value;
                cbo.Items.Add(value);
                cboCol.Items.Add(value);
                grid.CurrentCell.Value = value;
            }
        }

        protected void comboBox_TextChanged(object sender, EventArgs args)
        {
            DataGridViewComboBoxEditingControl cbo = (DataGridViewComboBoxEditingControl)sender;
            if (cbo.Text.Trim() == string.Empty) return;

            DataGridView grid = cbo.EditingControlDataGridView;
            string value = cbo.Text;
            if (String.IsNullOrEmpty(value))
                return;

            for (int i = 0; i < value.Length; i++)
            {
                if ((value[i] < 'a' || value[i] > 'z') && (value[i] < 'A' || value[i] > 'Z'))
                    return;
            }

            for (int i = 0; i < cbo.Items.Count; i++)
            {
                Type t = cbo.Items[i].GetType();
                string s = t.GetProperty("PersonPYM").GetValue(cbo.Items[i], null).ToString();
                if (s == cbo.Text)
                {
                    //cbo.TextChanged -= new EventHandler(comboBox_TextChanged);
                    cbo.SelectedIndex = i;
                    cbo.SelectedItem = Items[i];
                    cbo.Text = t.GetProperty(cbo.DisplayMember).GetValue(cbo.Items[i], null).ToString();
                    grid.CurrentCell.Value = t.GetProperty(cbo.ValueMember).GetValue(cbo.Items[i], null).ToString();
                    //cbo.TextChanged += new EventHandler(comboBox_TextChanged);
                    break;
                }
            }
        }
    }
}
